package com.thb.pay.manage.web.agent;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thb.pay.api.agent.AgentChannelServiceApi;
import com.thb.pay.api.agent.AgentInfoServiceApi;
import com.thb.pay.api.agent.vo.req.AgentChannelReq;
import com.thb.pay.api.agent.vo.resp.AgentChannelVo;
import com.thb.pay.api.agent.vo.resp.AgentInfoVo;
import com.thb.pay.api.payment.PayProductServiceApi;
import com.thb.pay.api.payment.vo.resp.PayProductVo;
import com.thb.pay.core.annotation.OperationLog;
import com.thb.pay.core.exception.RRException;
import com.thb.pay.core.utils.R;
import com.thb.pay.manage.comm.controller.BaseController;
import com.thb.pay.manage.enums.AgentExceptionEnum;
import com.thb.pay.manage.enums.PayExceptionEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


/**
 * @类名称 AgentAccountController
 * @类描述 <pre>代理商通道配置信息控制类</pre>
 * @作者 lucky
 * @创建时间 2019/10/12
 * @版本 v1.01
 * </pre>
 */
@RestController
@RequestMapping("api/sys/agentChannel")
public class AgentChannelController extends BaseController {

    @Autowired
    private AgentInfoServiceApi agentInfoServiceApi;

    @Autowired
    private AgentChannelServiceApi agentChannelServiceApi;

    @Autowired
    private PayProductServiceApi payProductServiceApi;

    @GetMapping("/list")
    public R list(@RequestParam("agentId") Long agentId) {
        List<AgentChannelVo> resultDataList = Lists.newArrayList();
        List<AgentChannelVo> agentPassageList = agentChannelServiceApi.findListByAgentId(agentId);
        Map<Long, AgentChannelVo> agentPassageMap = Maps.newHashMap();
        for (AgentChannelVo agentPassage : agentPassageList) {
            agentPassageMap.put(agentPassage.getProductId(), agentPassage);
        }
        // 得到所有支付产品
        List<Long> payProductIds = Lists.newArrayList(agentPassageMap.keySet());
        List<PayProductVo> payProductList = payProductServiceApi.findListByProductIds(payProductIds);
        for (PayProductVo product : payProductList) {
            AgentChannelVo agentPassage = agentPassageMap.get(String.valueOf(product.getId()));
            if (agentPassage == null) {
                agentPassage = new AgentChannelVo();
                agentPassage.setAgentId(agentId);
                agentPassage.setProductId(product.getId());
                agentPassage.setAgentRate(product.getAgentRate());
                agentPassage.setStatus(0);
                agentPassage.setSelected(0);
            }
            agentPassage.setProductName(product.getName());
            resultDataList.add(agentPassage);
        }
        return R.ok(resultDataList);
    }

    /**
     * 查询
     *
     * @return
     */
    @GetMapping("/get")
    public R get(@RequestParam("agentId") Long agentId, @RequestParam("productId") Long productId) {

        PayProductVo payProductVo = payProductServiceApi.getById(productId);
        if (payProductVo == null) {
            throw RRException.build(PayExceptionEnum.PAY_PRODUCT_NOT_EXIST);
        }
        AgentChannelVo agentChannelVo = agentChannelServiceApi.getByAgentIdAndProductId(agentId, productId);
        if (agentChannelVo == null) {
            agentChannelVo = new AgentChannelVo();
            agentChannelVo.setAgentId(agentId);
            agentChannelVo.setProductId(productId);
            agentChannelVo.setAgentRate(payProductVo.getAgentRate());
            agentChannelVo.setStatus(0);//未选中
            agentChannelVo.setSelected(0);
        }
        agentChannelVo.setProductName(payProductVo.getName());
        return R.ok(agentChannelVo);
    }

    /**
     * @param agentChannelReq
     * @Description 添加或修改代理商支付通道 【上级代理商非一级代理，有的情况才能配置】
     * @Author lucky
     * @data 2019/10/14 22:12
     */
    @PostMapping("/addOrUpdate")
    @OperationLog(value = "添加代理商支付通道")
    public R addOrUpdate(@RequestBody AgentChannelReq agentChannelReq) {
        AgentInfoVo agentInfoVo = agentInfoServiceApi.getByAgentId(agentChannelReq.getAgentId());
        if (agentInfoVo == null) {
            throw RRException.build(AgentExceptionEnum.AGENT_NOT_EXIST);
        }
        PayProductVo payProductVo = payProductServiceApi.getById(agentChannelReq.getProductId());
        if (payProductVo == null) {
            throw RRException.build(PayExceptionEnum.PAY_PRODUCT_NOT_EXIST);
        }

        Long parentAgentId = agentInfoVo.getParentId();
        if (parentAgentId != null && parentAgentId != 0) {
            //校验费率 非一级代理商, 子级代理商的费率,大于父级的代理商费率(父级代理商的费率不能为null)
            AgentChannelVo parentAgentChannelVo = agentChannelServiceApi.getByAgentIdAndProductId(parentAgentId, agentChannelReq.getProductId());
            BigDecimal parentAgentRate = parentAgentChannelVo != null ? parentAgentChannelVo.getAgentRate() : null;
            if (parentAgentRate == null) {
                throw RRException.build(PayExceptionEnum.PAY_AGENT_RATE_NOT_EXIST);
            }
            if (parentAgentRate.compareTo(agentChannelReq.getAgentRate()) == 1) {
                throw RRException.build(PayExceptionEnum.PAY_AGENT_RATE_SET_ERROR);
            }
        }
        AgentChannelVo agentPassage = agentChannelServiceApi.getByAgentIdAndProductId(agentChannelReq.getAgentId(), agentChannelReq.getProductId());
        //添加或修改
        agentChannelReq.setId(agentPassage != null ? agentPassage.getId() : null);
        agentChannelServiceApi.addOrUpdate(agentChannelReq);
        return R.ok();
    }

}
